﻿
Public Class numbercheck
    Public Const functionname As String = "noc"
    Public Const regularExpression As String = "nocr"

    Public Shared Sub registerRegularExpression(ByVal inlineScript As inlineScript)
        assert(Not inlineScript Is Nothing, "inlineScript is nothing.")
        var.invoke(inlineScript, regularExpression, "/^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0|[1-9]\d*)$/")
    End Sub

    Public Shared Sub register(ByVal inlineScript As inlineScript)
        assert(Not inlineScript Is Nothing, "inlineScript is nothing.")

        registerRegularExpression(inlineScript)
        inlineScript.addscript("numbercheck", "function " + functionname + "(i){return " + _
                               regularExpression + ".test(i);}")
    End Sub

    Public Shared Function invoke(ByVal inlineScript As inlineScript, ByVal input As String, _
                                  Optional ByRef actionTarget As String = Nothing) As String
        If Not inlineScript Is Nothing Then
            register(inlineScript)
        End If

        actionTarget += functionname + "(" + input + ")"
        Return actionTarget
    End Function
End Class

Public Class numberreplace
    Public Const functionname As String = "norp"
    Public Const replaceExpression As String = "norpr"

    Public Shared Sub registerReplaceExpression(ByVal inlineScript As inlineScript)
        assert(Not inlineScript Is Nothing, "inlineScript is nothing.")
        var.invoke(inlineScript, replaceExpression, "/[^-\d.+]/gi")
    End Sub

    Public Shared Sub register(ByVal inlineScript As inlineScript)
        assert(Not inlineScript Is Nothing, "inlineScript is nothing.")

        registerReplaceExpression(inlineScript)
        inlineScript.addscript("numberreplace", "function " + functionname + "(i){" + _
                               ifelse.invoke(numbercheck.invoke(inlineScript, "i"), _
                                             "return i", _
                                             "return i.replace(" + replaceExpression + ","""")"))
    End Sub

    Public Shared Function invoke(ByVal inlineScript As inlineScript, ByVal input As String, _
                                  Optional ByRef actionTarget As String = Nothing) As String
        If Not inlineScript Is Nothing Then
            register(inlineScript)
        End If

        actionTarget += functionname + "(" + input + ")"
        Return actionTarget
    End Function
End Class

Public Class numberreplace2
    Public Const functionname As String = "norp2"

    Public Shared Sub register(ByVal inlineScript As inlineScript)
        assert(Not inlineScript Is Nothing, "inlineScript is nothing.")

        numberreplace.registerReplaceExpression(inlineScript)
        inlineScript.addscript("numberreplace2", "function " + functionname + "(i,c){" + _
                                 ifelse.invoke("!" + numbercheck.invoke(inlineScript, "i"), _
                                               "c(i.replace(" + numberreplace.replaceExpression + ",""""))") + "}")
    End Sub

    Public Shared Function invoke(ByVal inlineScript As inlineScript, _
                                  ByVal input As String, ByVal callback As String, _
                                  Optional ByRef actionTarget As String = Nothing) As String
        If Not inlineScript Is Nothing Then
            register(inlineScript)
        End If

        actionTarget += functionname + "(" + input + "," + callback + ")"
        Return actionTarget
    End Function
End Class